package com.easygem.controller.front;

import com.easygem.common.model.system.User;
import com.easygem.common.response.ResponseBean;
import com.easygem.common.vo.front.TaskPackageVO;
import com.easygem.common.vo.system.PageVO;
import com.easygem.front.service.CustomerService;
import com.easygem.front.service.OutUserService;
import com.easygem.front.service.TaskPackageResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

/**
 * @author 肖智
 * @version 1.0
 * @Description 离职用户控制层
 * @date 2025/3/4
 */
@Api(tags = "标签")
@RestController
@RequestMapping("/outUser")
@Transactional
public class OutUserController {
    @Autowired
    private OutUserService outUserService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private TaskPackageResourceService taskPackageResourceService;

    //查询所有离职员工
    @ApiOperation(value = "查询所有离职员工", notes = "查询所有离职员工")
    @GetMapping("/selectOutUserList")
    public ResponseBean<PageVO<User>> selectOutUserList(Integer pageNum, Integer pageSize,String accountName) {
        PageVO<User> pageVO = outUserService.selectOutUserList(pageNum, pageSize,accountName);
        return ResponseBean.success(pageVO);
    }

    //客户转移
    @ApiOperation(value = "批量分配资源给用户", notes = "将资源分配给用户")
    @PutMapping("/clientShift")
    public Integer ClientShift(@RequestBody Map<String, Long[]> map) {
        // 调用服务层进行资源分配
        Long[] resourceIds = map.get("resourceIds");
        Long[] userIds = map.get("userIds");
        System.out.println("资源集合" + Arrays.toString(resourceIds));
        System.out.println("用户集合" + Arrays.toString(userIds));
        Integer result = customerService.clientShift(resourceIds, userIds);
        System.out.println("分配成功：" + result);
        return result;
    }

    //根据用户id得到资源集合
    @ApiOperation(value = "根据用户id得到资源集合", notes = "根据用户id得到资源集合")
    @GetMapping("/getResourceIdByUserId")
    public Long[] getResourceIdByUserId(@RequestParam Long userId) {
        System.out.println("用户ID：" + userId);
        Long[] resourceIdList = outUserService.getResourceIdByUserId(userId);
        System.out.println("资源集合ID：" + Arrays.toString(resourceIdList));
        return resourceIdList;
    }

    //根据用户id得到客户集合
    @ApiOperation(value = "根据用户id得到资源集合", notes = "根据用户id得到资源集合")
    @GetMapping("/getClientIdByUserId")
    public Long[] getClientIdByUserId(Long userId) {
        return outUserService.getClientIdByUserId(userId);
    }

    //删除客户
    @ApiOperation(value = "删除客户", notes = "根据客户ID删除客户")
    @PutMapping("/deleteResource")
    @ResponseBody
    public ResponseBean<Integer> deleteResource(@RequestBody Map<String, Long[]> map) {
        Long[] resourceIds = map.get("resourceIds");
        System.out.println("资源集合" + Arrays.toString(resourceIds));
        Integer result = outUserService.deleteResource(resourceIds);
        return ResponseBean.success(result);
    }

    /**
     * 回收资源
     * @param taskPackageName 任务包名称
     * @return 操作信息
     */
    @ApiOperation(value = "回收资源" , notes = "回收资源")
    @PutMapping("/resourceRecycling")
    @ResponseBody
    public ResponseBean<String> resourceRecycling(@RequestParam String taskPackageName,
                                                  @RequestParam Long oldUserId,
                                                  @RequestParam Long newUserId) {
        TaskPackageVO taskPackageVO = new TaskPackageVO();
        taskPackageVO.setTaskPackageName(taskPackageName);
        String success = taskPackageResourceService.resourceRecycling(taskPackageVO,oldUserId,newUserId);
        return ResponseBean.success(success);
    }

    /**
     * 回收客户
     * @param taskPackageName 任务包名称
     * @return 操作信息
     */
    @ApiOperation(value = "回收客户" , notes = "回收客户")
    @PutMapping("/customerRecycling")
    @ResponseBody
    public ResponseBean<String> clientRecycling(@RequestParam String taskPackageName,
                                                @RequestParam Long oldUserId,
                                                @RequestParam Long newUserId) {
        TaskPackageVO taskPackageVO = new TaskPackageVO();
        taskPackageVO.setTaskPackageName(taskPackageName);
        String success = taskPackageResourceService.customerRecycling(taskPackageVO,oldUserId,newUserId);
        return ResponseBean.success(success);
    }

}
